import os
import re
import sys
import time
import logging
import pymysql
import datetime

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import TimeoutException, NoSuchWindowException, WebDriverException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

class SaveEmail():
    login_url = 'http://mail.eyes-control.com/webmail7.5/webmail.php?r=site/index/domain/eyes-control.com'
    def __init__(self, username, str_id, str_time):
        self.username = username
        self.password = 'SHANGHAI.yankong.'
        self.str_id = str_id
        self.str_time = str_time
        self.find_id = False
        self.access = True
        if str_id:
            self.index = -1
        else:
            self.index = 0
        chrome_options = webdriver.ChromeOptions()
        # 无头模式
        # chrome_options.add_argument('--headless')
        # chrome_options.add_argument("--window-size=1920,1080")
        # chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
        # chrome_options.add_experimental_option('useAutomationExtension', False)
        # chrome_options.add_argument('--proxy-server=%s' % 'http://116.196.85.166:3128')
        # 修改文件保存路径
        save_dir = os.path.join('/media/ubuntu/08f29263-3882-43db-96fd-febb6f2e2e5f/mail_data1', username)
        # if not os.path.exists(save_dir):
        #     os.makedirs(save_dir)
        # chrome_options.add_argument("--incognito")
        # 设置禁止加载图片, 文件保存文件夹
        prefs = {"profile.managed_default_content_settings.images":2, 'download.default_directory': save_dir, 'profile.default_content_settings.popups': 0}
        chrome_options.add_experimental_option("prefs", prefs)
        # chrome_options.add_argument('--no-sandbox')
        self.browser = webdriver.Chrome(chrome_options=chrome_options, service_log_path="log.txt")
        # 最长等待时间为60s
        self.wait = WebDriverWait(self.browser, 120, 2)
        self.wait1 = WebDriverWait(self.browser, 120, 2)

    # 用户登录，进入邮箱主页
    def login(self):
        self.browser.get(self.login_url)
        # 输入用户名
        username = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="username"]')))
        username.send_keys(self.username)
        # 输入密码
        password = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="secretkeyshow"]')))
        password.send_keys(self.password)
        # 点击登录按钮
        login_button = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="login_submit_btn"]')))
        login_button.click()

    # 获取所有历史邮件
    def get_history_mail(self):
        # 显示所有年份
        try:
            all_year = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="sideMenu"]/div[2]/ul/li[11]/a')))
            all_year.click()
        # 没有历史邮件
        except Exception as e:
            logging.info('没有历史邮箱')
            update_sql = 'update user1 set complete="1" where username="%s"'%self.username
            cursor.execute(update_sql)
            db.commit()
            return None
        # time.sleep(2)
        list_year = self.wait.until(EC.presence_of_all_elements_located((By.XPATH, '//*[@id="sideMenu"]/div[2]/ul/li[11]/ul/li/ul/li/a')))
        for year in list_year:
            # 判断帐号是否被封
            if self.access:
                js = year.get_attribute("onclick")
                if self.str_time:
                    # 匹配年月
                    result = re.search('\d{6}', js).group(0)
                    if result > self.str_time:
                        print(result)
                        logging.info(result)

                        continue
                    else:
                        # 加载每个月的邮箱
                        self.browser.execute_script(js)
                        # time.sleep(1)
                        print('开始匹配到的：next_page')
                        logging.info('开始匹配到的：next_page')
                        self.next_page()
                        self.str_time = ''
                else:
                    # 加载每个月的邮箱
                    self.browser.execute_script(js)
                    # time.sleep(1)
                    print('str_time不存在的：next_page')
                    logging.info('str_time不存在的：next_page')
                    self.next_page()
            else:
                print('帐号被封')
                logging.info('帐号被封')
                break
        if self.access:
            logging.info('邮件全部下载完成')
            update_sql = 'update user1 set complete="1" where username="%s"'%self.username
            cursor.execute(update_sql)
            db.commit()

    # 下载邮件并判断是否有下一页，有下一页则继续下载
    def next_page(self):
        if self.access:
            if self.str_id and self.index==-1 and not self.find_id:
                time.sleep(2)
                mail_time_xpath = '//*[@id="__p8_list_main"]/div/table/tbody/tr/td[5]/div'
                list_mail_time = self.wait.until(EC.presence_of_all_elements_located((By.XPATH, mail_time_xpath)))
                while True:
                    for index, mail_info in enumerate(list_mail_time):
                        # print(index, mail_info.get_attribute('id'))
                        try:
                            str_id = mail_info.get_attribute('id')
                            # print(str_id)
                        except Exception:
                            time.sleep(2)
                            try:
                                str_id = mail_info.get_attribute('id')
                            except Exception:
                                print('3333333')
                                logging.info('没有获取到id，3333333')
                                # self.access = False
                                return None
                        # print(self.str_id, str_id)
                        # str_time = mail_info.text
                        if str_id == self.str_id:
                            self.index = index
                            self.find_id = True
                            if self.index != -1:
                                list_mail_xpath = '//*[@id="__p8_list_main"]/div/table/tbody/tr[@id]'
                                mail_length = len(self.wait.until(EC.presence_of_all_elements_located((By.XPATH, list_mail_xpath))))
                                logging.info('当前页邮件数：%s'%mail_length)
                                index = self.index
                                print('匹配的self.index:', index)
                                for i in range(index, mail_length): # 0,1 不是邮件
                                    # 进入一封邮件，并切换到邮件窗口
                                    print('switch_window之前')
                                    self.switch_window(list_mail_xpath, i)
                                    print('switch_window之后')
                                    try:
                                        # 点击更多操作
                                        print('点击更多1')
                                        time.sleep(2)
                                        logging.info('点击更多1')
                                        more_botton = self.wait.until(EC.presence_of_element_located((By.XPATH, 'id("layout_top")/div[2]/a[8]')))
                                        print('匹配到更多', more_botton)
                                        logging.info('匹配到更多%s'%more_botton)
                                        more_botton.click()
                                        # 导出邮件
                                        self.wait.until(EC.presence_of_element_located((By.XPATH, '//div[@class="address_tip_menu"]/ul/li[@class="li_underline"]/a'))).click()
                                        logging.info('导出邮件')
                                        # 关闭当前窗口
                                        self.close_window()
                                        logging.info('关闭当前窗口')
                                    except TimeoutException as e:
                                        print('111111')
                                        logging.info('1111111111, ')
                                        self.access = False
                                        return None
                                    self.index = 0
                                    # next_page_element = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="__p8_list_bottom"]/div/div[2]/a[3]')))
                                    # next_page_class = next_page_element.get_attribute("class")
                                    # # 判断是否是最后一页
                                    # if 'disabled' not in next_page_class:
                                    #     next_page_element.click()
                                    #     self.next_page()
                    try:
                        time.sleep(2)
                        self.browser.find_element_by_xpath('//*[@id="__p8_list_bottom"]/div/div[2]/a[3][@class="nextPage disabled"]')
                        print('匹配到最后一页')
                        logging.info('匹配到最后一页')

                        break
                    except Exception as e:

                        next_page_element = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="__p8_list_bottom"]/div/div[2]/a[3]')))
                        next_page_class = next_page_element.get_attribute("class")
                        # 判断是否是最后一页
                        if 'disabled' not in next_page_class:
                            print('进入下一页1')
                            logging.info('进入下一页1')
                            next_page_element.click()
                            self.next_page()
                        else:
                            break

            elif self.index != -1:
                time.sleep(2)
                list_mail_xpath = '//*[@id="__p8_list_main"]/div/table/tbody/tr[@id]'
                mail_length = len(self.wait.until(EC.presence_of_all_elements_located((By.XPATH, list_mail_xpath))))
                print(mail_length)
                logging.info('当前页邮件数：%s'%mail_length)
                # logging.info(mail_length)
                # index = self.index if self.index else 0
                # print('起始index:', index)

                for i in range(mail_length): # 0,1 不是邮件
                    # 进入一封邮件，并切换到邮件窗口
                    try:
                        self.switch_window(list_mail_xpath, i)
                    except IndexError:
                        print('超出索引')
                        logging.info('超出索引')
                        continue
                    except Exception  as e:
                        print('其他错误')
                        logging.info('其他错误')
                        continue
                    try:
                        # 点击更多操作
                        time.sleep(2)
                        print('点击更多3')
                        logging.info('点击更多3')
                        more_botton = self.wait.until(EC.presence_of_element_located((By.XPATH, 'id("layout_top")/div[2]/a[8]')))
                        more_botton.click()
                        # 导出邮件
                        logging.info('导出邮件')
                        self.wait.until(EC.presence_of_element_located((By.XPATH, '//div[@class="address_tip_menu"]/ul/li[@class="li_underline"]/a'))).click()
                        # 关闭当前窗口
                        logging.info('关闭当前窗口')
                        self.close_window()
                    except TimeoutException as e:
                        print('2222222')
                        self.access = False
                        return None
                    except WebDriverException as e:
                        # 关闭当前窗口
                        # self.close_window()
                        time.sleep(4)
                        logging.info('点击更多4')
                        more_botton = self.wait.until(EC.presence_of_element_located((By.XPATH, 'id("layout_top")/div[2]/a[8]')))
                        more_botton.click()
                        # 导出邮件
                        logging.info('导出邮件')
                        self.wait.until(EC.presence_of_element_located((By.XPATH, '//div[@class="address_tip_menu"]/ul/li[@class="li_underline"]/a'))).click()
                        # 关闭当前窗口
                        logging.info('关闭当前窗口')
                        self.close_window()
                    # time.sleep(55)
                # self.index = 0
                next_page_element = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="__p8_list_bottom"]/div/div[2]/a[3]')))
                next_page_class = next_page_element.get_attribute("class")
                # 判断是否是最后一页
                if 'disabled' not in next_page_class:
                    print('进入下一页2')
                    logging.info('进入下一页2')
                    next_page_element.click()
                    self.next_page()

    def close_window(self):
        self.browser.switch_to_default_content()
        close_element = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="navigateArea"]/div/div[2]/ul/li[@class="tab_item active"]/a')))
        close_element.click()
    
    # 进入一封邮件，并切换到邮件窗口
    def switch_window(self, list_mail_xpath, i):
        print('下载文件index:', i)
        logging.info('下载文件index:%s'%i)

        mail_time_xpath = '//*[@id="__p8_list_main"]/div/table/tbody/tr/td[5]/div'

        # 添加日志信息
        try:
            mail_time = self.wait.until(EC.presence_of_all_elements_located((By.XPATH, mail_time_xpath)))[i]
        except Exception:
            time.sleep(2)
            logging.info('mail_time报错')
            mail_time = self.wait1.until(EC.presence_of_all_elements_located((By.XPATH, mail_time_xpath)))[i]
        str_id = mail_time.get_attribute('id')
        str_text = mail_time.text.split(' ')[0].replace('-', '')[:-2]

        update_sql = 'update user1 set str_id="%s",str_time="%s" where username="%s"'%(str_id, str_text ,self.username)
        cursor.execute(update_sql)
        db.commit()

        log_info = str_id + ' ' + str_text
        logging.info(log_info)

        # 点击一封邮件
        try:
            time.sleep(2)
            logging.info('点击一封邮件')
            mail_botton = self.wait.until(EC.presence_of_all_elements_located((By.XPATH, list_mail_xpath)))[i]
            mail_botton.click()
        except Exception:
            time.sleep(2)
            logging.info('点击一封邮件报错')
            mail_botton = self.wait.until(EC.presence_of_all_elements_located((By.XPATH, list_mail_xpath)))[i]
            mail_botton.click()
        # 点击更多操作，动态加载导出邮件按钮
        # time.sleep(2)
        logging.info('获取iframe_id')
        iframe_id = self.wait.until(EC.presence_of_element_located((By.XPATH, '//iframe'))).get_attribute("id")
        # # 切换到当前iframe
        print('切换到iframe:', iframe_id)
        logging.info('切换到iframe:%s'%iframe_id)
        self.browser.switch_to_frame(iframe_id)

def is_add(userinfo):
    # print(int(userinfo[3])+5 < int(datetime.datetime.now().strftime('%Y%m%d%H')))
    return int(userinfo[3])+5 < int(datetime.datetime.now().strftime('%Y%m%d%H'))

if __name__ == '__main__':
    db = pymysql.connect("192.168.50.149","root","root","test",charset='utf8')
    cursor = db.cursor()
    # cursor.execute('SELECT username,str_id,str_time,last_time,crawling FROM user where complete=0 and crawling=0')
    # tuple_userinfo = cursor.fetchall()
    
    # tuple_userinfo = [['chenwei', 'asdf1234?'], ]
    # 保存日志的文件夹
    if not os.path.exists('./log'):
        os.makedirs('./log')
    # print('time.sleep(14400)')
    # time.sleep(14400)
    # tuple_userinfo = filter(is_add, tuple_userinfo)
    
    # 遍历所有用户
    while True:
        try:
            cursor.execute('SELECT username,str_id,str_time,last_time,crawling FROM user1 where complete=0 and crawling=0')
            tuple_userinfo = cursor.fetchall()
            tuple_userinfo = list(filter(is_add, tuple_userinfo))
            print(len(tuple_userinfo))

            if len(tuple_userinfo):
                username, str_id, str_time, last_time, crawling = tuple_userinfo[0]
                str_now_time = datetime.datetime.now().strftime('%Y%m%d%H')
                last_time = int(last_time)+5
                print(last_time, int(str_now_time))
                if last_time < int(str_now_time):
                    print(username, str_id, str_time, last_time)
                    # 初始化日志对象
                    logging.basicConfig(
                        level = logging.INFO, # 日志级别
                        format = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', # 日志格式, 时间、代码所在文件名、代码行号、日志级别名字、日志信息
                        datefmt = '%a, %d %b %Y %H:%M:%S', # 打印日志的时间
                        # filename = 'log/'+username+'.log', # 日志文件存放的目录（目录必须存在）及日志文件名
                        filename = 'log/info.log', # 日志文件存放的目录（目录必须存在）及日志文件名
                        filemode = 'a' # 打开日志文件的方式
                    )
                    logging.info('%s, %s, %s, %s'%(username, str_id, str_time, last_time))

                    # 把当前用户更新为正在下载
                    update_sql = 'update user1 set crawling="%s" where username="%s"'%('1' ,username)
                    cursor.execute(update_sql)
                    db.commit()

                    email = SaveEmail(username, str_id, str_time)
                    email.login()
                    email.get_history_mail()
                    email.browser.quit()

                    # 把当前用户更新为没有在下载
                    update_sql = 'update user1 set crawling="%s" where username="%s"'%('0' ,username)
                    cursor.execute(update_sql)
                    db.commit()

                    # 更新用户的最后下载时间
                    update_sql = 'update user1 set last_time="%s" where username="%s"'%(str_now_time ,username)
                    cursor.execute(update_sql)
                    db.commit()
                else:
                    print(username, '未满5小时')
            else:
                print('没有匹配的用户, time.sleep(7200)')
                time.sleep(7200)
        except Exception as e:
            print('报错')
    
    # for username, str_id, str_time, last_time, crawling in tuple_userinfo:
        # str_now_time = datetime.datetime.now().strftime('%Y%m%d%H')
        # last_time = int(last_time)+5
        # print(last_time, int(str_now_time))
        # if last_time < int(str_now_time):
        #     print(username, str_id, str_time, last_time)
        #     # 初始化日志对象
        #     logging.basicConfig(
        #         level = logging.INFO, # 日志级别
        #         format = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', # 日志格式, 时间、代码所在文件名、代码行号、日志级别名字、日志信息
        #         datefmt = '%a, %d %b %Y %H:%M:%S', # 打印日志的时间
        #         filename = 'log/'+username+'.log', # 日志文件存放的目录（目录必须存在）及日志文件名
        #         filemode = 'a' # 打开日志文件的方式
        #     )
        #     update_sql = 'update user1 set crawling="%s" where username="%s"'%('1' ,username)
        #     cursor.execute(update_sql)
        #     db.commit()

        #     email = SaveEmail(username, str_id, str_time)
        #     email.login()
        #     email.get_history_mail()
        #     email.browser.quit()

        #     update_sql = 'update user1 set crawling="%s" where username="%s"'%('0' ,username)
        #     cursor.execute(update_sql)
        #     db.commit()

        #     update_sql = 'update user1 set last_time="%s" where username="%s"'%(str_now_time ,username)
        #     cursor.execute(update_sql)
        #     db.commit()
        # else:
        #     print('未满5小时')
            
